<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/7
 * Time: 11:00
 */

use Workerman\Lib\Timer;
use Workerman\Worker;

require_once "./workerman/Autoloader.php";

Worker::$logFile = '/tmp/workerman.log';

//worker1实例有4个进程 进程的ID编号分别为 0 1 2 3

$worker1 = new Worker("tcp://0.0.0.0:8687");

//为worker1实例设置启动4个进程

$worker1->count = 4;

//为每个进程启动时打印当前id编号即 $worker1->id

$worker1->onWorkerStart = function ($worker1) {

    echo "worker1->id=={$worker1->id}";
    //定时每10秒就向所有的客户端连接发送当前的服务器时间
    Timer::add(10, function () use ($worker1) {
        foreach ($worker1->connections as $connection) {
            $connection->send(time());

        }
    });
};
$worker1->onConnect = function ($connection) {
    echo 'one new connection from ip' . $connection->getRemoteIp() . "\n";
};

$worker1->onClose = function ($connection) {

    echo $connection->getRemoteIp() . '已经 leave';
};

//// worker实例2有两个进程，进程id编号将分别为0、1
//
//$worker2 = new Worker('tcp://0.0.0.0:8686');
//
//// 设置启动2个进程
//
//$worker2->count = 2;
//
//// 每个进程启动后打印当前进程id编号即 $worker2->id
//
//$worker2->onWorkerStart = function ($worker2) {
//
//    echo "worker2->id={$worker2->id}\n";
//};

// 运行worker
Worker::runAll();